home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / EtherPEG / (sources) / EtherPEG.cp next >
Encoding:
Text File  |  2000-06-24  |  6.6 KB  |  329 lines

  1. #include <Types.h>
  2. #include <Memory.h>
  3. #include <Quickdraw.h>
  4. #include <Fonts.h>
  5. #include <Events.h>
  6. #include <Menus.h>
  7. #include <TextEdit.h>
  8. #include <MacWindows.h>
  9. #if UNIVERSAL_INTERFACES_VERSION >= 0x0330
  10. #include <ControlDefinitions.h>
  11. #endif
  12. #include <Dialogs.h>
  13. #include <OSUtils.h>
  14. #include <ToolUtils.h>
  15. #include <Devices.h>
  16. #include <StandardFile.h>
  17. #include <Movies.h>
  18. #include <Sound.h>
  19. #include <QuickTimeComponents.h>
  20. #include <ImageCompression.h>
  21. #include <Resources.h>
  22. #include <FixMath.h>
  23.  
  24. #include "SortFrames.h"
  25. #include "Promiscuity.h"
  26.  
  27. extern void createWindow(void);
  28. extern void idleWindow(void);
  29. extern void destroyWindow(void);
  30.  
  31. Boolean gDone = false;
  32. Boolean gDrewJPEG = false;
  33.  
  34. enum {
  35.     kAppleMenuID            = 128,
  36.         kAppleMenuAbout            = 1,
  37.     kFileMenuID                = 129,
  38.         kFileMenuQuit            = 1
  39.     
  40. };
  41.  
  42. static void doMenu( long menuSelection )
  43. {
  44.     short whichMenu = HiWord(menuSelection);
  45.     short whichMenuItem = LoWord(menuSelection);
  46.     
  47.     switch (whichMenu) {
  48.         case kAppleMenuID:
  49.             switch (whichMenuItem) {
  50.                 case kAppleMenuAbout:
  51.                     Alert(128, nil);
  52.                     break;
  53.  
  54.                 default:
  55.                     {
  56.                     Str255 daName;
  57.                     GetMenuItemText(GetMenuHandle(kAppleMenuID), whichMenuItem, daName);
  58.                     OpenDeskAcc(daName);
  59.                     }
  60.                     break;
  61.             }
  62.             break;
  63.  
  64.         case kFileMenuID:
  65.             switch (whichMenuItem) {
  66.                 case kFileMenuQuit:
  67.                     gDone = true;
  68.                     break;
  69.             }
  70.             break;
  71.     }
  72. }
  73.  
  74. int main( void )
  75. {
  76.     GrafPtr wmgrPort;
  77.     
  78.     InitGraf(&qd.thePort);
  79.     InitFonts();
  80.     InitWindows();
  81.     InitMenus();
  82.     TEInit();
  83.     InitDialogs(nil);
  84.     InitCursor();
  85.     MaxApplZone();
  86.     
  87.     GetWMgrPort( &wmgrPort );
  88.     SetPort( wmgrPort );
  89.     EnterMovies();
  90.  
  91.     SetMenuBar(GetNewMBar(128));
  92.     AppendResMenu(GetMenuHandle(kAppleMenuID), 'DRVR');
  93.     DrawMenuBar();
  94.  
  95.     createStash();
  96.     initPromiscuity();
  97.     createWindow();
  98.     
  99.     while (gDone == false) {
  100.         EventRecord theEvent;
  101.         WindowPtr whichWindow;
  102.         short windowPart;
  103.         UInt32 timeoutTicks;
  104.  
  105.         idleWindow();
  106.         
  107.         gDrewJPEG = false;
  108.         timeoutTicks = TickCount() + 5;
  109.         while( !gDrewJPEG && ( TickCount() < timeoutTicks ) ) {
  110.             idlePromiscuity();
  111.         }
  112.         
  113.         WaitNextEvent(everyEvent, &theEvent, 0, nil);
  114.         
  115.         switch (theEvent.what) {
  116.             case updateEvt:
  117.                 whichWindow = (WindowPtr)theEvent.message;
  118.                 SetPort(whichWindow);
  119.                 BeginUpdate(whichWindow);
  120.                 //drawWindow(whichWindow);
  121.                 EndUpdate(whichWindow);
  122.                 break;
  123.             
  124.             case keyDown:
  125.                 if (theEvent.modifiers & cmdKey) {
  126.                     doMenu(MenuKey(theEvent.message & charCodeMask));
  127.                 }
  128.                 break;
  129.             
  130.             case mouseDown:
  131.                 windowPart = FindWindow(theEvent.where, &whichWindow);
  132.  
  133.                 switch (windowPart) {
  134.                     case inDrag:
  135.                         DragWindow(whichWindow, theEvent.where, &qd.screenBits.bounds);
  136.                         break;
  137.  
  138.                     case inGoAway:
  139.                         if (TrackGoAway(whichWindow, theEvent.where))
  140.                             gDone = true;
  141.                         break;
  142.  
  143.                     case inContent:
  144.                         if (whichWindow != FrontWindow())
  145.                         {
  146.                             SelectWindow(whichWindow);
  147.                         }
  148.                         else
  149.                         {
  150.                             //clickWindow(whichWindow, theEvent.where);
  151.                         }
  152.                         break;
  153.  
  154.                     case inMenuBar:
  155.                         doMenu(MenuSelect(theEvent.where));
  156.                         break;
  157.                 }
  158.                 break;
  159.         }
  160.     }
  161.     
  162.     destroyWindow();
  163.     termPromiscuity();
  164.     destroyStash();
  165.     
  166.     return 0;    
  167. }
  168.  
  169. WindowPtr window = nil;
  170. GraphicsImportComponent gripJ = 0;
  171. GraphicsImportComponent gripG = 0;
  172.  
  173. void createWindow( void )
  174. {
  175.     OSErr err;
  176.     Rect windowBounds;
  177.  
  178.     windowBounds = qd.screenBits.bounds;
  179.     InsetRect(&windowBounds, 30, 50);
  180.     
  181.     window = NewCWindow( nil, &windowBounds, "\pEtherPEG", true, documentProc, 
  182.             (WindowPtr)-1, true, 0);
  183.     
  184.     SetPort( window );
  185.     
  186.     err = OpenADefaultComponent( GraphicsImporterComponentType, kQTFileTypeJPEG, &gripJ );
  187.     if( err ) DebugStr( "\p couldn't open jpeg grip." );
  188.     err = OpenADefaultComponent( GraphicsImporterComponentType, kQTFileTypeGIF, &gripG );
  189.     if( err ) DebugStr( "\p couldn't open gif grip." );
  190. }
  191.  
  192. void DisplayJPEGAndDisposeHandle( Handle jpeg );
  193.  
  194. void idleWindow()
  195. {
  196.     //•• call pmb...
  197. #define TEST_GRIP 0
  198. #if TEST_GRIP
  199.     Handle h;
  200.     h = Get1Resource( 'JPEG', 128 );
  201.     if( h ) {
  202.         DetachResource( h );
  203.         DisplayJPEGAndDisposeHandle( h );
  204.     }
  205. #endif // TEST_GRIP
  206. }
  207.  
  208. void destroyWindow()
  209. {
  210.     if( gripJ )
  211.         CloseComponent( gripJ );
  212.     if( gripG )
  213.         CloseComponent( gripG );
  214. }
  215.  
  216. void DisplayJPEGAndDisposeHandle( Handle jpeg )
  217. {
  218.     OSErr err;
  219.     Rect naturalBounds;
  220.     MatrixRecord matrix;
  221.     SInt32 gapH, gapV;
  222.     Fixed scaleH, scaleV;
  223.     Rect boundsRect;
  224.     GraphicsImportComponent grip;
  225.     
  226.     if( !jpeg ) return;
  227.     
  228.     if( 'G' == **jpeg )
  229.         grip = gripG;
  230.     else
  231.         grip = gripJ;
  232.     
  233.     err = GraphicsImportSetDataHandle( grip, jpeg );
  234.     if( err ) goto bail;
  235.     err = GraphicsImportGetNaturalBounds( grip, &naturalBounds );
  236.     if( err ) goto bail;
  237.     
  238.     gapH = window->portRect.right - naturalBounds.right;
  239.     gapV = window->portRect.bottom - naturalBounds.bottom;
  240.     
  241.     if( gapH >= 0 ) {
  242.         gapH = ((UInt16)Random()) % gapH;
  243.         scaleH = fixed1;
  244.     }
  245.     else {
  246.         gapH = 0;
  247.         scaleH = FixDiv( window->portRect.right, naturalBounds.right );
  248.     }
  249.     
  250.     if( gapV >= 0 ) {
  251.         gapV = ((UInt16)Random()) % gapV;
  252.         scaleV = fixed1;
  253.     }
  254.     else {
  255.         gapV = 0;
  256.         scaleV = FixDiv( window->portRect.bottom, naturalBounds.bottom );
  257.     }
  258.     
  259.     // need to use smaller scale of the two, and then recalc the other gap.
  260.     if( scaleH > scaleV ) {
  261.         scaleH = scaleV;
  262.         gapH = window->portRect.right - FixMul(scaleH, naturalBounds.right);
  263.         gapH = ((UInt16)Random()) % gapH;
  264.     } else if( scaleH < scaleV ) {
  265.         scaleV = scaleH;
  266.         gapV = window->portRect.bottom - FixMul(scaleV, naturalBounds.bottom);
  267.         gapV = ((UInt16)Random()) % gapV;
  268.     }
  269.     
  270.     SetIdentityMatrix( &matrix );
  271.     ScaleMatrix( &matrix, scaleH, scaleV, 0, 0 );
  272.     TranslateMatrix( &matrix, gapH<<16, gapV<<16 );
  273.  
  274.     err = GraphicsImportSetMatrix( grip, &matrix );
  275.     if( err ) goto bail;
  276.  
  277.     err = GraphicsImportDraw( grip );
  278.     if( err ) goto bail;
  279.     
  280.     err = GraphicsImportGetBoundsRect( grip, &boundsRect );
  281.     if( err ) goto bail;
  282.     InsetRect( &boundsRect, -1, -1 );
  283.     SetPort( window );
  284.     FrameRect( &boundsRect );
  285.     
  286. bail:
  287.     DisposeHandle( jpeg );
  288.     gDrewJPEG = true;
  289. }
  290.  
  291.  
  292. enum { 
  293.     kBlobSize = 6
  294. };
  295. void showBlob( short n )
  296. {
  297.     static Boolean first = true;
  298.     static Rect r, rx;
  299.     static RGBColor black = { 0,0,0 };
  300.     static RGBColor white = { 0xffff,0xffff,0xffff };
  301.     static RGBColor blue = { 0,0,0xffff };
  302.     static RGBColor green = { 0,0x8000,0 };
  303.     static RGBColor yellow = { 0xffff,0xffff,0 };
  304.     
  305.     SetPort( window );
  306.     RGBForeColor( 0 == n ? &yellow :
  307.                   1 == n ? &black :
  308.                   2 == n ? &green :
  309.                              &blue );
  310.     
  311.     if( first || r.right > window->portRect.right ) {
  312.         first = false;
  313.         r.left = 1;
  314.         r.bottom = window->portRect.bottom - 1;
  315.         r.top = r.bottom - kBlobSize;
  316.         r.right = r.left + kBlobSize;
  317.     }
  318.     PaintOval( &r );
  319.     RGBForeColor( &black );
  320.     
  321.     r.left += kBlobSize + 1;
  322.     r.right += kBlobSize + 1;
  323.     
  324.     rx = r;
  325.     rx.right += 10 * (kBlobSize + 1);
  326.     EraseRect( &rx );
  327. }
  328.  
  329.